//editdata.js
var common = require("../../../../../../utils/common.js");
//获取应用实例
var app = getApp()
Page({
  data:{
    type:null,
    id:null,
    name:null,
    remark:null,
    title:null,
    btnSaveLoading:false,
    btnDeleteLoading:false,
    saveText:"保存",
    deleteText:"删除"
  },
  onLoad: function (options) {
    var that=this
    that.setData({
      type:options.type,
      id:options.id,
      name:options.name,
      remark:options.remark
    })
    if(options.type==1){
      wx.setNavigationBarTitle({
        title: '支出类型'
      })
      that.setData({
        title:'支出类型'
      })
    }else if(options.type==2){
      wx.setNavigationBarTitle({
        title: '收入类型'
      })
      that.setData({
        title:'收入类型'
      })
    }else if(options.type==3){
      wx.setNavigationBarTitle({
        title: '成员'
      })
      that.setData({
        title:'成员'
      })
    }else{
      wx.setNavigationBarTitle({
        title: '卡种'
      })
      that.setData({
        title:'卡种'
      })
    }
  },
  btnSave: function (e){
    var that = this
    // 设置按钮加载状态
    this.setData({
      btnSaveLoading:true,
      saveText:"正在保存"
    })
    that.saveData(function(res){
        //更新按钮状态
        that.setData({
          btnSaveLoading:false,
          saveText:"保存"
        })
      if(res.status == 1){
        // 刷新recordParam数据
        app.getRecordParam(function(paramRes){
          if(paramRes.status==1){
            //刷新上一页数据
            var pages = getCurrentPages();
            if(pages.length > 1){
                //上一个页面实例对象
                var prePage = pages[pages.length - 2];
                switch(parseInt(that.data.type)){
                  case 1://支出
                    prePage.expenseData()
                  break;//收入
                  case 2:
                    prePage.incomeData()
                  break;//成员
                  case 3:
                    prePage.memberData()
                  break;
                  case 4://卡种
                    prePage.cardtypeData()
                  break;

                }
            }
            wx.navigateBack({
              delta: 1
            })
          }else{
            app.errorToGetToken()
          }
        })
      }
    })
  },
  btnDelete: function (e){
    wx.showModal({
      title: '警告',
      content: '确定要删除吗？',
      success: function(res) {
        return
      }
    })
    var that = this
    // 设置按钮加载状态
    this.setData({
      btnDeleteLoading:true,
      deleteText:"正在删除"
    })
    that.deleteData(function(res){
        //更新按钮状态
        that.setData({
          btnDeleteLoading:false,
          deleteText:"删除"
        })
      if(res.status == 1){
        // 刷新recordParam数据
        app.getRecordParam(function(paramRes){
          if(paramRes.status==1){
            //刷新上一页数据
            var pages = getCurrentPages();
            if(pages.length > 1){
                //上一个页面实例对象
                var prePage = pages[pages.length - 2];
                switch(parseInt(that.data.type)){
                  case 1://支出
                    prePage.expenseData()
                  break;//收入
                  case 2:
                    prePage.incomeData()
                  break;//成员
                  case 3:
                    prePage.memberData()
                  break;
                  case 4://卡种
                    prePage.cardtypeData()
                  break;

                }
            }
            wx.navigateBack({
              delta: 1
            })
          }else{
            app.errorToGetToken()
          }
        })
      }
    })
  },
  nameInput: function(e) {
    this.setData({
      name: e.detail.value
    })
  },
  remarkInput: function(e) {
    this.setData({
      remark: e.detail.value
    })
  },
  deleteData: function (cb){
    var that = this
    wx.request({
      url: common.apiUrl()+'deleteType',
      data: {
        token:app.globalData.wangcaiToken,
        type:that.data.type,
        id:that.data.id
      },
      success: function(res){
        if(res.data.status==1){
          wx.showToast({
            title: '删除成功',
            icon: 'success',
            success: function(e){
              typeof cb == "function" && cb(res.data)
            }
          })
        }else{
          app.errorToGetToken()
        }
      }
    })
  },
  saveData: function (cb){
    var that = this
    var url = null
    var returnRes = null
    var remark = that.data.remark
    if(remark=="undefined"||remark==null){
      remark = ""
    }
    if(that.data.id==null){
    // 新增
      url=common.apiUrl()+'addType'
    }else{
    // 修改
      url=common.apiUrl()+'editType'
    }
    if(that.data.name==""||that.data.name=="undefined"||that.data.name==null) {
      // console.log(kind)
      wx.showModal({
        title: '错误提示',
        content: that.data.title+'不能为空',
        showCancel:false
      })
      returnRes = {
      "status":-1,
      "msg":"error"
      }
      typeof cb == "function" && cb(returnRes)
      return
    }
    if((that.data.name+"").length>4&&(parseInt(that.data.type))<3) {
      // console.log(kind)
      wx.showModal({
        title: '错误提示',
        content: that.data.title+'不能超过4个字',
        showCancel:false
      })
      returnRes = {
      "status":-1,
      "msg":"error"
      }
      typeof cb == "function" && cb(returnRes)
      return
    }
    wx.request({
      url: url,
      data: {
        token:app.globalData.wangcaiToken,
        type:that.data.type,
        id:that.data.id,
        name:that.data.name,
        remark:remark
      },
      method:'POST',
      header: {
      'content-type': 'application/x-www-form-urlencoded'
        },
      success: function(res){
        if(res.data.status==1){
          wx.showToast({
            title: '添加成功',
            icon: 'success',
            success: function(e){
              typeof cb == "function" && cb(res.data)
            }
          })
        }else{
          app.errorToGetToken()
        }
      }
    })
  }
})